﻿using System;
using System.Web;
using System.Text;
using System.Data.SQLite;

namespace Com.Toook.Common
{
    /// <summary>
    /// Sqlite辅助类
    /// </summary>
    public class SqliteHelper
    {
        static SqliteHelper()
        {
        }
        /// <summary>
        /// 获取Sqlite数据库连接对象, 默认devDB.db
        /// </summary>
        /// <returns></returns>
        public static SQLiteConnection GetConnection()
        {
            SQLiteConnection con = new SQLiteConnection(GetConStr());
            return con;
        }
        /// <summary>
        /// 获取dbName指定的Sqlite数据库连接对象
        /// </summary>
        /// <returns></returns>
        public static SQLiteConnection GetConnection(string dbName)
        {
            SQLiteConnection con = new SQLiteConnection(GetConStr(dbName));
            return con;
        }
        /// <summary>
        /// 关闭数据库资源
        /// </summary>
        /// <param name="con">数据库连接，没有传null</param>
        /// <param name="reader">SQLiteDataReader，没有传null</param>
        public static void CloseDbResource(SQLiteConnection con, SQLiteDataReader reader)
        {
            if(reader != null)
            {
                try
                {
                    reader.Close();
                }
                catch (Exception ex)
                {
                    LogHelper.Log.Error("SqliteHelper.CloseDbResource reader 错误", ex);
                }
            }
            if(con != null)
            {
                try
                {
                    con.Close();
                }
                catch (Exception ex)
                {
                    LogHelper.Log.Error("SqliteHelper.CloseDbResource con 错误", ex);
                }
            }
        }
        /// <summary>
        /// 生成电影Id.10位长度.如 1302109876
        /// </summary>
        /// <returns></returns>
        public static int GetMovieId()
        {
            DateTime now = DateTime.Now;
            int y = now.Year - 2013;//前3位为400 * y + now.DayOfYear,即每年加400.2013为0
            int day = y * 400 + now.DayOfYear;
            int second = now.Hour * 60 * 60 + now.Minute * 60 + now.Second;
            string movieId = day.ToString() + second.ToString("D5") + GetFixedMovieOrderStr();
            
            //string movieId = DateTime.Now.ToString("yyMMdd") + GetFixedMovieOrderStr();
            return int.Parse(movieId);
        }
        /// <summary>
        /// 生成新闻Id.10位长度.如 1302109876  33661440 3365 86400
        /// </summary>
        /// <returns></returns>
        public static int GetNewsId()
        {
            DateTime now = DateTime.Now;
            int y = now.Year - 2013;//前3位为400 * y + now.DayOfYear,即每年加400.2013为0
            int day = y * 400 + now.DayOfYear;
            int second = now.Hour * 60 * 60 + now.Minute * 60 + now.Second;
            string newsId = day.ToString() + second.ToString("D5") + GetFixedMovieOrderStr();       
            //string newsId = DateTime.Now.ToString("yyMMdd") + GetFixedNewsOrderStr();
            return int.Parse(newsId);
        }
        /// <summary>
        /// 获取新闻日期(当前日期)
        /// </summary>
        /// <returns></returns>
        public static string GetNewsPubDateNow()
        {
            return DateTime.Now.ToString("yyyy-MM-dd");
        }
        /// <summary>
        /// 生成固定(1/4)位长度的电影顺序号字符串
        /// </summary>    
        /// <returns>固定长度为(1/4)的字符串</returns>
        private static string GetFixedMovieOrderStr()
        {
            if(movieIdOrder > movieIdMax)
            {
                movieIdOrder = 0;
            }

            string result = movieIdOrder.ToString("D1");//D4
            movieIdOrder++;

            return result;
        }
        /// <summary>
        /// 生成固定(1/4)位长度的新闻顺序号字符串
        /// </summary>    
        /// <returns>固定长度为(1/4)的字符串</returns>
        private static string GetFixedNewsOrderStr()
        {
            if(newsIdOrder > newsIdMax)
            {
                newsIdOrder = 0;
            }

            string result = newsIdOrder.ToString("D1");//D4
            newsIdOrder++;

            return result;
        }
        private static string GetConStr()
        {
            if(connectionString == null)
            {
                connectionString = "Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString() //HttpContext.Current.Request.PhysicalApplicationPath
                    + "App_Data\\devDB.db;password=nest99MOVIE;Pooling=true;FailIfMissing=false;";
            }
            return connectionString;
        }
        /// <summary>
        /// 动态生成连接字符串
        /// </summary>
        /// <param name="dbName"></param>
        /// <returns></returns>
        private static string GetConStr(string dbName)
        {
            connectionStringDynamic = "Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString() //HttpContext.Current.Request.PhysicalApplicationPath
                    + "App_Data\\" + dbName + ".db;password=nest99MOVIE;Pooling=true;FailIfMissing=false;";

            return connectionStringDynamic;
        }
        /// <summary>
        /// 生成笑话分类ID。 9位：2位分类 + 1位年 + 3位天 + 4位序号
        /// </summary>
        /// <returns></returns>
        public static int GetJokeTypeId(string jokeType)
        {
            DateTime now = DateTime.Now;
            int y = now.Year - 2013;
            int day = now.DayOfYear;

            string jokeId = jokeType + y.ToString() + day.ToString("D3") + GetFixedJokeOrderStr();            
            return int.Parse(jokeId);
        }
        /// <summary>
        /// 获取笑话序号，固定3位
        /// </summary>
        /// <returns></returns>
        private static string GetFixedJokeOrderStr()
        {
            if (jokeIdOrder > jokeIdMax)
            {
                jokeIdOrder = 0;
            }

            string result = jokeIdOrder.ToString("D3");
            jokeIdOrder++;

            return result;
        }
        /// <summary>
        /// 笑话Id 后(1/4)位顺序号
        /// </summary>
        private static int jokeIdOrder = 0;
        /// <summary>
        /// 电影Id 后(1/4)位顺序号
        /// </summary>
        private static int movieIdOrder = 0;
        /// <summary>
        /// 新闻Id 后(1/4)位顺序号
        /// </summary>
        private static int newsIdOrder = 0;
        /// <summary>
        /// 笑话Id 后(1/4)位顺序号最大值
        /// </summary>
        private static readonly int jokeIdMax = 9; //9999;
        /// <summary>
        /// 电影Id 后(1/4)位顺序号最大值
        /// </summary>
        private static readonly int movieIdMax = 9; //9999;
        /// <summary>
        /// 新闻Id 后(1/4)位顺序号最大值
        /// </summary>
        private static readonly int newsIdMax = 9; //9999;
        /// <summary>
        /// Sqlite数据库默认连接字符串
        /// </summary>
        private static string connectionString =
            "Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory.ToString() //HttpContext.Current.Request.PhysicalApplicationPath
          + "App_Data\\devDB.db;password=nest99MOVIE;Pooling=true;FailIfMissing=false;";
        /// <summary>
        /// 根据传入的数据库名称，生成Sqlite数据库连接字符串
        /// </summary>
        private static string connectionStringDynamic = string.Empty;
    }
}
